<template>
  <div>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="8" :sm="12">
              <a-form-item label="搜索类型" style="display: flex">
                <div style="display: flex">
                  <a-select v-model="queryParam.searchType" placeholder="请选择" style="width: 130px">
                    <a-select-option v-for="(item, index) in condition.searchTypeList" :key="index" :value="item.key">{{ item.value }}</a-select-option>
                  </a-select>
                  <a-input v-model="queryParam.searchValue" placeholder="请输入" style="margin-left: 5px" />
                </div>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="公开状态">
                <a-select v-model="queryParam.isPublish" placeholder="请选择" :getPopupContainer="triggerNode => triggerNode.parentNode">
                  <a-select-option v-for="(item, index) in condition.publishList" :key="index" :value="item.key">{{ item.value }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="品牌">
                <a-select v-model="queryParam.brandList" placeholder="请选择" :getPopupContainer="triggerNode => triggerNode.parentNode" mode="multiple" show-search>
                  <a-select-option v-for="(item, index) in condition.brandList" :key="index" :value="item.key">{{ item.value }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="素材类型">
                <a-select v-model="queryParam.classifyList" placeholder="请选择" :getPopupContainer="triggerNode => triggerNode.parentNode">
                  <a-select-option v-for="(item, index) in condition.classifyList" :key="index" :value="item.key">{{ item.value }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="时间">
                <a-range-picker style="width: 100%" :show-time="{ format: 'YYYY-MM-DD HH:mm:ss' }" :placeholder="['开始时间', '结束时间']" v-model="regTime" @change="onChange" />
              </a-form-item>
            </a-col>
            <!-- <a-col :md="8" :sm="12">
              <a-form-item label="门店">
                <a-select v-model="queryParam.status" placeholder="请选择" :getPopupContainer="triggerNode => triggerNode.parentNode">
                  <a-select-option v-for="(item, index) in condition.classifyList" :key="index" :value="item.key">{{ item.value }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col> -->
            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons" :style="{ float: 'right', overflow: 'hidden' }">
                <a-button type="primary" @click="search">查询</a-button>
                <a-button style="margin-left: 8px" @click="reset">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <s-table ref="table" size="default" :rowKey="record => record.id" :columns="columns" :scroll="{ x: 1300 }" :data="loadData" :pagination="pagination" :bordered="false" style="margin-top: 24px">
        <span slot="brand" slot-scope="text, record, index">
          <div v-if="record.brand">
            <a-tag style="margin: 2px" color="blue" v-for="(label, index) in record.brand.split(',')" :key="index">
              {{ label }}
            </a-tag>
          </div>

          <span v-else></span>
        </span>

        <span slot="price" slot-scope="text, record, index">
          <img :src="record.videoDetail.coverUrl" style="width: 50px; height: 50px" @click="commodity(record)" v-if="record.videoDetail.coverUrl" />
          <img :src="record.imgUrls[0]" style="width: 50px; height: 50px" @click="commodity(record)" v-else />
        </span>
        <span slot="isPublish" slot-scope="text, record">
          <span>{{ record.isPublish == 2 ? '非公开' : '已公开' }}</span>
        </span>
        <span slot="action" slot-scope="text, record, index">
          <a @click="showModel(record.id)">查看</a>
          <a-divider type="vertical" />
          <a-popconfirm placement="topLeft" ok-text="Yes" title="确定公开吗？" cancel-text="No" @confirm="handleUpOrDown(1, record.id)" v-if="record.isPublish === 2">
            <a>公开</a>
          </a-popconfirm>
          <a-popconfirm placement="topLeft" ok-text="Yes" title="确定非公开吗？" cancel-text="No" @confirm="handleUpOrDown(2, record.id)" v-if="record.isPublish === 1">
            <a>取消公开</a>
          </a-popconfirm>
          <a-divider type="vertical" v-if="record.isPublish === 1" />
          <a @click="download(record)" v-if="record.isPublish === 1">下载</a>
        </span>
      </s-table>
    </a-card>
    <DetailModel ref="detailModel"></DetailModel>
    <CommodityModel ref="commodityModel"></CommodityModel>
  </div>
</template>
<script>
import { STable } from '@/views/box/components'
import DetailModel from './components/detailModel.vue'
import CommodityModel from './components/commodityModel.vue'
import { conditionList, themeList, themeStatus, themeDown, themeMedia } from '@/api/pos/match'
import Vue from 'vue'
import { USER_INFO } from '@/store/mutation-types'
const columns = [
  {
    title: '标题',
    dataIndex: 'name',
    width: 140
  },
  {
    title: '效果图',
    dataIndex: 'price',
    scopedSlots: { customRender: 'price' },
    width: 80
  },
  {
    title: '发布导购',
    dataIndex: 'publisherName',
    width: 100
  },
  {
    title: '工号',
    dataIndex: 'workNo',
    width: 100
  },
  {
    title: '门店',
    dataIndex: 'storeName',
    width: 160
  },
  {
    title: '所属品牌',
    dataIndex: 'brand',
    scopedSlots: { customRender: 'brand' },
    width: 240
  },
  {
    title: '商品数',
    dataIndex: 'productNum',
    width: 80
  },
  {
    title: '类型',
    dataIndex: 'classifyName',
    width: 100
  },
  {
    title: '状态',
    dataIndex: 'isPublish',
    scopedSlots: { customRender: 'isPublish' },
    width: 120
  },
  {
    title: '发布时间',
    dataIndex: 'createTime',
    width: 200
  },
  {
    title: '操作',
    scopedSlots: { customRender: 'action' },
    width: 220,
    fixed: 'right'
  }
]
export default {
  components: {
    STable,
    DetailModel,
    CommodityModel
  },
  data() {
    return {
      columns,
      queryParam: {
        channelType: 'guideTheme',
        size: 10,
        current: 1
      },
      regTime: [],
      searchTypeList: [],
      pagination: {
        pageSize: 10,
        page: 1,
        current: 1,
        total: 0,
        showTotal: total => `共有 ${total} 条数据`
      },
      tableData: [],
      condition: {},
      redisKey: '',
      userInfo: Vue.ls.get(USER_INFO),
      loadData: parameter => {
        const queryParam = JSON.parse(JSON.stringify(this.queryParam))
        const data = {}
        data.themeListQto = queryParam

        if (queryParam.classifyList) {
          data.themeListQto.classifyList = [queryParam.classifyList]
        }

        data.themeListQto.current = parameter.pageNo
        data.themeListQto.size = parameter.pageSize
        // delete queryParam.regTime
        return themeList(data.themeListQto).then(res => {
          if (res.code === 200) {
            this.pagination.current = res.data.current
            const { records, total, size, current } = res.data

            const data = {
              page: {
                count: total,
                pageNo: current,
                pageSize: size,
                pages: Math.ceil(total / size)
              },
              data: records
            }

            return data
          } else {
            this.$message.error(res.msg || res.message)
            const data = {
              page: {
                pageNo: 0,
                count: 0,
                pageSize: 10
              },
              data: []
            }
            return data
          }
        })
      }
    }
  },
  mounted() {
    this.postCondition()
    this.conditionList()
  },
  methods: {
    conditionList() {
      const queryParam = JSON.parse(JSON.stringify(this.queryParam))
      const data = {}
      data.themeListQto = queryParam

      if (queryParam.classifyList) {
        data.themeListQto.classifyList = [queryParam.classifyList]
      }

      // data.themeListQto.current = parameter.pageNo
      // data.themeListQto.size = parameter.pageSize
      // delete queryParam.regTime
      return themeList(data.themeListQto).then(res => {
        if (res.code === 200) {
          const pagination = { ...this.pagination }
          pagination.pageSize = res.data.size
          pagination.total = res.data.total
          this.pagination = pagination
          this.tableData = res.data.records
        } else {
          this.$message.error(res.msg || res.message)
        }
      })
    },
    closeBig() {
      this.visibleImgs = false
    },
    handleUpOrDown(type, id) {
      const data = {
        id: id,
        isPublish: type, // :1公开，0非公开
        isDelete: false
      }
      themeStatus(data).then(res => {
        if (res.code == 200) {
          setTimeout(() => {
            this.$refs.table.refresh()
          }, 1000)
        } else {
          this.$message.warning(res.msg)
        }
      })
    },
    postCondition() {
      let params = {
        channelType: 'guideTheme'
      }
      conditionList(params).then(res => {
        if (res.code == 200) {
          this.condition = res.data
        }
      })
    },
    showModel(id) {
      this.$refs.detailModel.show(id)
    },

    search() {
      this.$refs.table.refresh(true)
    },
    reset() {
      this.queryParam = {
        channelType: 'guideTheme',
        size: 10,
        current: 1
      }
      this.regTime = null

      this.$refs.table.refresh()
    },

    // 下载
    download(record) {
      let params = {
        imageList: record.imgUrls,
        lookId: record.lookId,
        videoUrl: record.videoDetail.videoUrl ? record.videoDetail.videoUrl : ''
      }

      themeDown(params).then(res => {
        if (res.code == 200) {
          this.redisKey = res.data
          this.pistThemeMedia(res.data)
        } else {
          this.$message.warning(res.msg)
        }
      })
    },
    pistThemeMedia(redisKey) {
      themeMedia(redisKey).then(res => {
        if (res.code == 200) {
          console.log(res.data)
          if (res.data == 'wait') {
            setTimeout(() => {
              this.pistThemeMedia(this.redisKey)
            }, 500)
          } else {
            window.location.href = res.data
          }

          // this.$message.success(res.msg)
        } else {
          this.$message.warning(res.msg)
        }
      })
    },
    // 商品
    commodity(record) {
      this.$refs.commodityModel.show(record)
    },
    onChange(value, dateString) {
      this.queryParam.startDate = dateString[0]
      this.queryParam.endDate = dateString[1]
    }
  }
}
</script>
<style scoped>
</style>